package com.seanLab.tool.TagSuggestModel;

import com.seanLab.domain.TagInfo;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;

/***
 * 混合各来源关键词，采用平均混合策略或加权平均策略等
 */
public class ModelKernelByWeighted extends ModelKernel {
    private double[] weights = new double[] {0.0, 1.0};
    private ModelKernelStrategy strategy = new ModelKernelStrategyMixed();

    public List<TagInfo> suggestTag(List<Feature> features, RawInfoType[] rawInfoTypes, int num) {
        List<List<TagInfo>> tagsList = subList(features, 20);
        for (List<TagInfo> tags : tagsList) {
            fillTagInfo(tags, rawInfoTypes[tagsList.indexOf(tags)]);
        }
        List<TagInfo> finalTags = strategy.doSuggest(tagsList, rawInfoTypes);
        if (finalTags.size() > num) {
            finalTags = finalTags.subList(0 ,num);
        }
        return fillTagID(finalTags);
    }

    public List<TagInfo> suggestTagForArticle(List<Feature> features, RawInfoType[] rawInfoTypes, int num) {
        List<List<TagInfo>> tagsList = subList(features, 20);
        List<TagInfo> finalTags = strategy.doSuggest(tagsList, rawInfoTypes);
        if (finalTags.size() > num) {
            finalTags = finalTags.subList(0 ,num);
        }
        return finalTags;
    }

    public List<TagInfo> suggestTagForArticleFromTitle(List<Feature> features, RawInfoType[] rawInfoTypes, int num) {
        List<TagInfo> titleTags = subList(features.get(0), 20);
        HashSet<String> titleMap = new HashSet<>();
        for (TagInfo tag : titleTags) {
            titleMap.add(tag.getTagName());
        }
        List<TagInfo> contentTags = subList(features.get(1), 20);
        List<TagInfo> ret = new ArrayList<>();
        for (TagInfo tag : contentTags) {
            if (titleMap.contains(tag.getTagName())) {
                ret.add(tag);
            }
        }
        if (ret.size() > num) {
            ret = ret.subList(0, num);
        }
        return ret;
    }

    private List<List<TagInfo>> subList(List<Feature> features, int subLength) {
        List<List<TagInfo>> tagsList = new ArrayList<>();
        for (Feature feature : features) {
            tagsList.add(subList(feature, subLength));
        }
        return tagsList;
    }

    private List<TagInfo> subList(Feature feature, int subLength) {
        List<TagInfo> subTags = ((FeatureInTags)feature).getFeature();
        if (subLength == 0) {
            return subTags;
        }
        if (subTags.size() > subLength) {
            subTags = subTags.subList(0, subLength);
        }
        return subTags;
    }

    public void setModelKernel(double[] weights) {
        this.weights = weights;
    }

    public void setStrategy(ModelKernelStrategy strategy) {
        this.strategy = strategy;
    }

    private List<TagInfo> fillTagInfo(List<TagInfo> rawTags, RawInfoType type) {
        if (type == null || rawTags == null || rawTags.size() == 0) {
            return rawTags;
        }
        for (TagInfo tag : rawTags) {
//            tag.setTagID("tagId-" + UUID.randomUUID().toString());
            switch (type) {
                case TITLE:
                    tag.setTagSource("文本抽取-Title");
                    break;
                case CONTENT:
                    tag.setTagSource("文本抽取-Content");
                    break;
                case PARA_ABOVE_IMAGE:
                    tag.setTagSource("文本抽取-Para-Above");
                    break;
                case PARA_UNDER_IMAGE:
                    tag.setTagSource("文本抽取-Para-Under");
                    break;
                case DESC_OF_IMAGE:
                    tag.setTagSource("文本抽取-Description");
                    break;
                case SEMANTIC_OF_IMAGE:
                    tag.setTagSource("文本抽取-Semantic");
                    break;
                case TAG:
                    tag.setTagSource("文本抽取-Tag");
                    break;
            }
//            tag.setTagSource("文本抽取");
        }
        return rawTags;
    }

    private List<TagInfo> fillTagID(List<TagInfo> tags) {
        for (TagInfo tag : tags) {
            tag.setTagID("tagId-" + UUID.randomUUID().toString());
        }
        return tags;
    }
}